//
//  ViewController.m
//  centerDemo
//
//  Created by winter on 2021/8/6.
//

#import "ViewController.h"
#import "Masonry.h"

@interface CencerCell : UICollectionViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *iconView;
@end

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic) NSInteger count;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = UIColor.lightGrayColor;
    bgView.layer.cornerRadius = 14;
    
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@100);
        make.left.equalTo(@16);
        make.right.equalTo(@-16);
        make.height.equalTo(@250);
    }];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [button setTitle:@"ADD" forState:(UIControlStateNormal)];
    [button setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(add) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.width.equalTo(@80);
        make.height.equalTo(@40);
        make.top.equalTo(bgView.mas_bottom).offset(30);
    }];
    
    UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [button2 setTitle:@"SUB" forState:(UIControlStateNormal)];
    [button2 setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    
    [button2 addTarget:self action:@selector(sub) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button2];
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.width.equalTo(@80);
        make.height.equalTo(@40);
        make.top.equalTo(button.mas_bottom).offset(30);
    }];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.textColor = UIColor.blackColor;
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"测试";
    
    [bgView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.height.equalTo(@65);
    }];
    
    CGFloat itemWidth = (([UIScreen mainScreen].bounds.size.width)-15-32)/4.0;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(itemWidth, 76);
    layout.minimumInteritemSpacing = 5;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    //隐藏滚动条
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    [collectionView registerClass:NSClassFromString(@"CencerCell")
       forCellWithReuseIdentifier:@"cell"];
    collectionView.alwaysBounceHorizontal = YES;
    collectionView.backgroundColor = UIColor.whiteColor;
    [bgView addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.top.equalTo(label.mas_bottom).offset(16);
        make.height.equalTo(@80);
    }];
    self.collectionView = collectionView;
}

- (void)add
{
    _count+=1;
    [self.collectionView reloadData];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_count-1 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionRight) animated:YES];
}

- (void)sub
{
    _count-=1;
    _count = MAX(0, _count);
    [self.collectionView reloadData];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_count-1 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionLeft) animated:YES];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
    return self.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                           cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CencerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLabel.text = [NSString stringWithFormat:@"index-%ld",indexPath.row];
    UIImage *image = [UIImage imageNamed:@"icon_autoscan_unkown_ble"];
    cell.iconView.image = image;
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    UICollectionViewFlowLayout *flowlayout = (UICollectionViewFlowLayout *) collectionViewLayout;
    CGFloat cellSpacing = flowlayout.minimumLineSpacing;
    CGFloat cellWidth = flowlayout.itemSize.width;
    NSInteger cellCount = [collectionView numberOfItemsInSection:section];
    CGFloat inset = (collectionView.bounds.size.width - (cellCount * cellWidth) - ((cellCount - 1)*cellSpacing)) * 0.5;
    inset = MAX(inset, 0.0);
    return UIEdgeInsetsMake(0.0, inset, 0.0, 0.0);
}
@end

@implementation CencerCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIImage *image = [UIImage imageNamed:@"icon_autoscan_unkown_ble"];
    _iconView = [[UIImageView alloc] initWithImage:image];
    [self addSubview:_iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.top.equalTo(@0);
        make.width.height.equalTo(@48);
    }];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = UIColor.lightGrayColor;
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom).offset(8);
        make.left.right.equalTo(@0);
        make.height.equalTo(@20);
    }];
    _titleLabel = label;
}

@end
